package dgut.party.business.common.util;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class JsonResult {
  private static String SUCCESS_KEY = "success";
  private static String CODE_KEY = "code";
  private static String DATA_KEY = "data";
  private static String MSG_KEY = "msg";

  public static String fillResultString(Integer status, String message, Object result) {
    JSONObject jsonObject = null;
    try {
      jsonObject = new JSONObject() {
        {
          put(SUCCESS_KEY, status);
          put(CODE_KEY, status);
          put(MSG_KEY, message);
          put(DATA_KEY, result);
        }
      };
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return jsonObject.toString();
  }

  private static Map<String, Object> jsonResultMap(Integer status, String message, Object content) {
    Map<String, Object> result = new HashMap<String, Object>();
    result.put(SUCCESS_KEY, Integer.valueOf("0").equals(status) ? true : false);
    result.put(CODE_KEY, status);
    result.put(MSG_KEY, message);
    result.put(DATA_KEY, content);
    return result;
  }

  public static Map<String, Object> successJsonResultMap(Object content) {
    return jsonResultMap(0, "success", content);
  }

  public static Map<String, Object> successJsonResultMap(String message, Object content) {
    return jsonResultMap(0, message, content);
  }

  public static Map<String, Object> successJsonResultMap(Integer status, String message, Object content) {
    return jsonResultMap(status, message, content);
  }

  public static Map<String, Object> failureJsonResultMap(Object content) {
    return jsonResultMap(-1, "success", content);
  }

  public static Map<String, Object> failureJsonResultMap(String message, Object content) {
    return jsonResultMap(-1, message, content);
  }

  public static Map<String, Object> failureJsonResultMap(Integer status, String message, Object content) {
    return jsonResultMap(status, message, content);
  }
}
